%%%----------------------------------------------------------------------------
%%% File         : echo.erl
%%% Author       : Rajesh Nair <rajesh.nair.in@gmail.com>
%%% Description  : A simple echo server which provides echoing service (content
%%%                as it is and reversing of content)
%%%
%%%----------------------------------------------------------------------------
-module(echo).
-author('Rajesh Nair <rajesh.nair.in@gmail.com>').
-behaviour(gen_server).



%%--- API functions of the server --------
-export([start/0,echo/1,revecho/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
	 terminate/2, code_change/3]).

%% Implementation of API functions ----------
start() ->
	gen_server:start_link({local,?MODULE},?MODULE,[],[]).

echo(Content) ->
	gen_server:call(?MODULE,{echo,Content}).


revecho(Content) ->
	gen_server:call(?MODULE,{revecho,Content}).


init([]) ->  {ok,started}.


%%-- implementation of handle_call function -----

handle_call({echo,Content},_From,State) ->
	Response = Content,	
	{reply,Response,State};

handle_call({revecho,Content},_From,State) ->
	Response = lists:reverse(Content),
	{reply,Response,State};

handle_call(_Message, _From, State) ->
    {reply, error, State}.

% We get compile warnings from gen_server unless we define these

handle_cast(_Message, Library) -> {noreply, Library}.
handle_info(_Message, Library) -> {noreply, Library}.
terminate(_Reason, _Library) -> ok.
code_change(_OldVersion, Library, _Extra) -> {ok, Library}.


